<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/OrderCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ReservetimeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/UsertypeCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");
require_once(dirname(__FILE__) . "/../dbcom/StaisCom.php");
require_once(dirname(__FILE__) . "/../dbcom/AppointCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ServiceinfoCom.php");
require_once(dirname(__FILE__) . "/../dbcom/C_serviceCom.php");

$log = new Logger("./../orderapi.log");
$common = new Common();
$ordercom = new OrderCom();
$reservetimecom = new ReservetimeCom();
$ragioncom = new RagionCom();
$usertypecom = new UsertypeCom();
$servicecom = new ServiceCom();
$staiscom = new StaisCom();
$appointcom = new AppointCom();
$serviceinfocom = new ServiceinfoCom();
$cservicecom = new CservicetCom();

if (!isset($_REQUEST['cont']) || empty($_REQUEST['cont'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot find cont"));
    exit();
}

switch ($_REQUEST['cont']) {
    case 1://下达订单
        $customerid = $_REQUEST['user_id'] ? $_REQUEST['user_id'] : null;
        $result = array("code" => 0, "msg" => "success", "data" => array());
        $servicejson = $_REQUEST['serviceinfo'] ? $_REQUEST['serviceinfo'] : null;
        if (empty($servicejson)) {
            echo json_encode(array("code" => 100, "msg" => "lose parameter"));
            exit();
        }
        //生成订单号
        $before = str_pad($customerid, 4, "0", STR_PAD_LEFT);
        $mid = date("YdmHis", time());
        $behind = $common->getRandomStr(4);
        $tradeno = $before . $mid . $behind;
        $servicearr = json_decode($servicejson, true);
        $totalfee = 0;
        foreach ($servicearr as $k => $v) {
            $service = $servicecom->getServiceById($v['id']);
            $result["data"]["servicelist"][$k]['service'] = $service['name'];
            $fee = "";
            if ($service['isncard'] == 0) {
                $fee = $service['fee'] * $v['number'];
                $result["data"]["servicelist"][$k]['number'] = $v['number'];
            } else {
                $fee = $service['fee'];
                $result["data"]["servicelist"][$k]['number'] = 20;
            }

            $totalfee += $fee;
            $result["data"]["servicelist"][$k]['fee'] = sprintf("%.2f", $fee / 100);
        }
        $time = time();
        $addid = $ordercom->addOrder($customerid, $tradeno, $time, $totalfee);
        if (!$addid) {
            echo json_encode(array("code" => 400, "msg" => "addorder failed"));
            exit();
        }
        $result["data"]["orderid"] = $addid;
        //根据orderid插入appoint表
//        $ares = $appointcom->addAppoint($addid, $reservetimeid, $time);
//        if (!$ares) {
//            echo json_encode(array("code" => 400, "msg" => "addappoint failed"));
//            exit();
//        }

        foreach ($servicearr as $k => $v) {
            //存储serviceinfo表
            $service1 = $servicecom->getServiceById($v['id']);
            if ($service1['isncard'] == 0) {
                $sres = $serviceinfocom->addServiceinfo($addid, $v['id'], $v['number'], 0);
            } else {
                $sres = $serviceinfocom->addServiceinfo($addid, $v['id'], 20, 1);
            }
        }
        //$myserviceinfo = serialize($serviceinfo);
        //echo $myserviceinfo;
        $result["data"]["totalfee"] = sprintf("%.2f", $totalfee / 100);
        $result["data"]["tradeno"] = $tradeno;
        echo json_encode($result);
        break;
    case 2://支付成功
        if (!isset($_REQUEST['orderid']) || empty($_REQUEST['orderid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot find orderid"));
            exit();
        }
        $orderid = $_REQUEST['orderid'];
        $res = $ordercom->changeOrderStatus($orderid, 2);
        if (!$res) {
            echo json_encode(array("code" => 400, "msg" => "changeorderstatus failed"));
            exit();
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    case 3://重新預約
        if ((!isset($_REQUEST['time']) || empty($_REQUEST['time'])) || (!isset($_REQUEST['reservetimeid']) || empty($_REQUEST['reservetimeid'])) 
                || (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) || (!isset($_REQUEST['address']) || empty($_REQUEST['address'])) 
                || (!isset($_REQUEST['usertype']) || empty($_REQUEST['usertype'])) || (!isset($_REQUEST['floor']) || empty($_REQUEST['floor'])) 
                || (!isset($_REQUEST['area']) || empty($_REQUEST['area'])) || (!isset($_REQUEST['street']) || empty($_REQUEST['street']))
                || (!isset($_REQUEST['community']) || empty($_REQUEST['community']))|| (!isset($_REQUEST['service']) || empty($_REQUEST['service']))
                || (!isset($_REQUEST['name']) || empty($_REQUEST['name']))|| (!isset($_REQUEST['codeid']) || empty($_REQUEST['codeid']))
                ||(!isset($_REQUEST['sex']) || empty($_REQUEST['sex']))||(!isset($_REQUEST['mobile']) || empty($_REQUEST['mobile']))) {
            echo json_encode(array("code" => 100, "msg" => "cannot find info"));
            exit();
        }
        $mobile = $_REQUEST['mobile'];
        $userid = $_REQUEST['userid'];
        $address = $_REQUEST['address'];
        $usertype = $_REQUEST['usertype'];
        $floor = $_REQUEST['floor'];
        $username = $_REQUEST['name'];
        $codeid = $_REQUEST['codeid'];
        $sex = $_REQUEST['sex'];
        $areaid = $_REQUEST['area'];
        $streetid = $_REQUEST['street'];
        $communityid = $_REQUEST['community'];
        $serviceid = $_REQUEST['service'];
        $reservetimeid = $_REQUEST['reservetimeid'];
        $time = $_REQUEST['time'];
        $time = strtotime($time);
        $allnum = $cservicecom->getAllNumber($userid, $serviceid);
        if(!$allnum){
            echo json_encode(array("code" => 400, "msg" => "get allnum fail"));
            exit();
        }
        if($allnum['number'] == 0 || $allnum['number'] < 0){
            echo json_encode(array("code" => 10, "msg" => "allnum is 0"));
            exit();
        }
        $allnum['number']--;
        $res1 = $cservicecom->changeNumber($userid, $serviceid, $allnum['number']);
        if(!$res1){
            echo json_encode(array("code" => 400, "msg" => "change allnum fail"));
            exit();
        }
        $brithday = substr($codeid, 6, 4);
        $nowyear = date("Y", time());
        $age = $nowyear - $brithday;
        $res = $appointcom->addAppoint($userid, $reservetimeid, $time, $address, $usertype, $floor, $areaid, $streetid, $communityid, $serviceid, $username, $codeid, $sex, $age,$mobile);
        if($res){
            echo json_encode(array("code"=>0,"msg"=>"success","data"=>array("serviceid"=>$serviceid,"number"=>$allnum)));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add appoint fail"));
        }
        exit();
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}

exit();
